package com.heima.search.service.impl;

import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.AppThreadUtil;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApUserSearch;
import com.heima.search.service.IApUserSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ApUserSearchServiceImpl implements IApUserSearchService {
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    @Async
    public void saveSearchRecord(String keyword, Integer userId, String equipmentId) {
        //判断用户是否保存了这个搜索的关键词
        Query query = new Query();
        if (userId != null && userId != 0){
            query.addCriteria(Criteria.where("userId").is(userId));
        }else {
            query.addCriteria(Criteria.where("equipmentId").is(equipmentId));
        }
        query.addCriteria(Criteria.where("keyword").is(keyword));
        ApUserSearch one = mongoTemplate.findOne(query, ApUserSearch.class);
        if (one == null){
            //新增
            ApUserSearch userSearch = new ApUserSearch();
            if (userId != null && userId != 0){
                userSearch.setUserId(userId);
            }else {
                userSearch.setEquipmentId(equipmentId);
            }
            userSearch.setKeyword(keyword);
            userSearch.setCreatedTime(new Date());
            mongoTemplate.save(userSearch);
        }
    }

    /*
        查询记录列表
     */
    @Override
    public ResponseResult load(ApArticleSearchDto dto) {
        //获取当前用户id
        User user = AppThreadUtil.getThread();
        Integer userId = null;
        if (user != null){
            userId = user.getUserId();
        }
        Query query = new Query();
        if (userId != null){
            query.addCriteria(Criteria.where("userId").is(userId));
        }else {
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }
        Pageable page = PageRequest.of(0, 5, Sort.Direction.DESC, "createdTime");
        //添加分页条件
        query.with(page);

        List<ApUserSearch> list = mongoTemplate.find(query, ApUserSearch.class);
        return ResponseResult.okResult(list);
    }

    /*
        删除查询记录
    */
    @Override
    public ResponseResult delete(ApArticleSearchDto dto) {
        List<ApUserSearch> hisList = dto.getHisList();
        List<String> list = new ArrayList<>();
        for (ApUserSearch userSearch : hisList) {
            list.add(userSearch.getId());
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("id").in(list));
        mongoTemplate.remove(query,ApUserSearch.class);
        return ResponseResult.okResult();
    }
}
